using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using Hsp.WebNovels.IRepository;
using Hsp.WebNovels.Common;
using Hsp.WebNovels.Model.Models;
using Hsp.WebNovels.Model.Dtos;
using Hsp.WebNovels.Model.Extensions;

namespace Hsp.WebNovels.Repository
{
    /// <summary>
    /// 小说信息数据仓储层
    /// </summary>
    public class NovelRepository : INovelRepository
    {
        
        public NovelRepository()
        {
            
        }

        #region 获取小说信息分页数据

        /// <summary>
        /// 获取小说信息分页数据
        /// </summary>
        /// <param name="paramList">分页参数</param>
        /// <returns></returns>
        public override DataTable PageData(Dictionary<string, string> paramList)
        {
            #region 分页参数处理

            var pagger = GetPagingParameters(paramList);

            #endregion

            string strQry = "";
            var strName = paramList.ContainsKey("Name") ? (paramList["Name"] ?? "") : "";
            if (!string.IsNullOrEmpty(strName))
            {
                //strQry = string.Format(@" AND (F_Name LIKE '%{0}%')", strName);
            }

            string strSql = string.Format(@"
	        ;WITH PageTb AS (
		        SELECT ROW_NUMBER() OVER (ORDER BY F_CreateTime DESC) RowNumber, 
			        *
		        FROM dbo.[Novels] WHERE (1 = 1) {2} )
	        SELECT * 
	        FROM PageTb a
	        CROSS JOIN (SELECT MAX(RowNumber) AS RecordCount FROM PageTb) AS b 
	        WHERE (a.RowNumber BETWEEN {0} AND {1});
	        ", pagger.Min, pagger.Max, strQry);

            return SqlHelper.GetDataTable(strSql, connectionString);
        }

        #endregion

    }
}

